package com.weixing.mall.provider.controller.front;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.MemAuth;
import com.weixing.mall.core.support.auth.MemInfo;
import com.weixing.mall.provider.api.model.vo.FrontGoodsVo;
import com.weixing.mall.provider.model.dto.GoodsVisitDto;
import com.weixing.mall.provider.model.vo.GoodsVisitVo;
import com.weixing.mall.provider.service.IGoodsService;
import com.weixing.mall.provider.service.IGoodsVisitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品模块-浏览足迹
 * </p>
 *
 * @author lgc
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: ${todo}
 * @date 2019/12/10 14:04
 */
@RestController
@RequestMapping("front/goodsvisit")
@Api(value = "前端:商品足跡Api", tags = "前端:商品足跡")
public class FrontGoodsVisitController extends BaseController {

    @Autowired
    private IGoodsVisitService iGoodsVisitService;
    @Autowired
    private IGoodsService iGoodsService;

    @ApiOperation(value = "搜索历史")
    @GetMapping("")
    public Result getListByMemberId(@MemInfo MemAuth auth) {
        List<Map<String,Object>> gvvlist = iGoodsVisitService.selectByMemberIdPage(initMpPage(),auth.getMid());
        IPage<Map<String,Object>> tt=new Page<Map<String,Object>>();
        tt.setTotal(iGoodsVisitService.selectMyHistory(auth.getMid()));
        tt.setRecords(gvvlist);
        return ResultUtil.success(tt);
    }

    @ApiOperation(value = "删除搜索历史")
    @GetMapping("/delByMemberId")
    public Result delByMemberId(@MemInfo MemAuth auth) {
        try {
            iGoodsVisitService.deleteByMemberId(auth.getMid());
            return ResultUtil.success();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResultUtil.error();
    }

    @ApiOperation(value = "新搜索历史")
    @GetMapping("/searchList")
    public Result getNewListByMemberId(@MemInfo MemAuth auth) {
        List<GoodsVisitVo> gvvlist = iGoodsVisitService.selectByMemberId(auth.getMid());
        return ResultUtil.success(gvvlist);
    }

    @ApiOperation(value = "保存搜索记录")
    @PostMapping("/save")
            public Result save(@MemInfo MemAuth auth,@RequestBody  GoodsVisitDto goodsVisitDto){
                try{
                    goodsVisitDto.setMemberId(auth.getMid());
                    IPage<FrontGoodsVo> list=iGoodsVisitService.save(goodsVisitDto,initMpPage());
                    return ResultUtil.success(list);
                }catch (Exception e){
                    e.printStackTrace();
                return  ResultUtil.error();
        }



    }
}
